<?php
$fields = $this->getFields();
$baseFields = $this->getBaseFields();
$rs = $this->lastResult;
?>
<div id="resultTable">
    <table width="100%" class="table table-bordered table-striped">
        <tr>
            <th>Carnet</th>
            <th>Nombre</th>
            <?php
            if(isset($_POST['baseFields'])){
                if(in_array("AIU_CODE", $baseFields)){
            ?>
            <th>AIU CODE</th>
            <?php
                }
                if(in_array("GENDER", $baseFields)){
            ?>
            <th>Género</th>
            <?php
                }
                if(in_array("BIRTHDATE", $baseFields)){
            ?>
            <th>Fecha de nacimiento</th>
            <?php
                }
                if(in_array("AGE", $baseFields)){
            ?>
            <th>Edad</th>
            <?php
                }
                if(in_array("FK_EMPRESA", $baseFields)){
            ?>
            <th>Empresa</th>
            <?php
                }
            }
            ?>
            <?php
                $i = 0;
                while($i < count($fields)){
                    $caption = $this->db->query_select("cllg_tablefield",sprintf("NAME='%s'",$fields[$i])); 
            ?>
                    <th><?php echo $caption[0]['CAPTION']?></th>
            <?php $i++;
                }
            if(!isEmpty(getParam("comoSeEntero"))){
            ?>
                    <th>
                        ¿Cómo se enteró?
                    </th>
            <?php
            }
            ?>
        </tr>
        <?php 
            if (count($this->lastResult) == 0){
                throw new Exception("<h5 style='color:blue;'>NO SE ENCONTRARON ALUMNOS ASIGNADOS A ESA CARRERA Y CICLO<h5>");
            }
            ?>
        <?php
        foreach ($rs as $r){
            
            $s = $this->db->query_select("cllg_student",sprintf("ID=%s",$r['FK_STUDENT']));
            $s = $s[0];
         
        ?>
        <tr>
            <td style="<?php echo ($r['STATUS'] == 0 ? "color:red; text-decoration:line-through" : "") ?>"><?php echo $s['ID'];?></td>
            <td style="<?php echo ($r['STATUS'] == 0 ? "color:red; text-decoration:line-through" : "") ?>"><?php echo $s['FIRST_NAME']. " " . $s['LAST_NAME'];?></td>
            <?php
            if(isset($_POST["baseFields"])){
                if(in_array("AIU_CODE", $baseFields)){
                    ?>
                    <td><?php echo ($s['AIU_CODE'] != NULL ? $s['AIU_CODE'] : "&nbsp;")?></td>
                    <?php
                }
                if(in_array("GENDER", $baseFields)){
                    ?>
                    <td><?php echo ($s['GENDER'] == "M" ? "Masculino" : ($s['GENDER'] == "F" ? "Femenino" : "&nbsp;"))?></td>
                    <?php
                }
                if(in_array("BIRTHDATE", $baseFields)){
                    ?>
                    <td><?php echo date("F d, Y", strtotime($s['BIRTHDATE']))?></td>
                    <?php
                }
                if(in_array("AGE", $baseFields)){
                    ?>
                    <td>
                    <?php
                        if($s['BIRTHDATE'] != NULL){
                            $cYear = date('Y');
                            print $cYear - date('Y', strtotime($s['BIRTHDATE'])); 
                        }
                        ?>
                    </td>
                    <?php
                }
                if(in_array("FK_EMPRESA", $baseFields)){
                    ?>
                    <td>
                    <?php
                        if($s['FK_EMPRESA'] != NULL){
                            $emp = $this->db->query_select("cllg_empresa",sprintf("ID=%s",$s['FK_EMPRESA']));
                            echo $emp[0]['NOMBRE'];
                        }  else {
                            echo "&nbsp;";
                        }
                    ?>
                    </td>
                    <?php
                }
            }
            ?>
            <?php
            $index = 0;
            while($index < count($fields)){
                $value = $this->db->query_select("cllg_table_valuebyfield",sprintf("FK_TABLEFIELD='%s' AND PK_RECORD=%s",$fields[$index],$r['FK_STUDENT']));
            ?>
            <td><?php echo ($value[0]['VALUE'] == NULL ? "&nbsp;" : $value[0]['VALUE']); ?></td>
            <?php
            $index++;
            }
            
            if(!isEmpty(getParam("comoSeEntero"))){
            ?>
                <td>
                <?php
                $selected = $this->db->queryToArray("
                        SELECT * 
                        FROM cllg_table_valuebyfield
                        WHERE FK_TABLEFIELD IN(
                            SELECT NAME
                            FROM cllg_tablefield
                            WHERE FK_TAB=9)
                        AND VALUE=1
                        AND PK_RECORD={$r['FK_STUDENT']}");
                 if(count($selected) != 0){
                     foreach ($selected as $sel){
                           $caption = $this->db->query_select("cllg_tablefield",sprintf("NAME='%s'",$sel['FK_TABLEFIELD']));
                           echo $caption[0]['CAPTION'];
                           echo ", ";
                       }
                 }else{
                     echo "&nbsp;";
                 }
                ?>
                </td>
            <?php
            }
            ?>
        </tr>
        <?php
        } 
        ?>
    </table>
</div>